import random
import time

from moviepy.editor import *
from moviepy.editor import AudioFileClip
from https.UploadPaths import upload_file
from service.Impl import videoServiceImpl
from jdk.openAi import repCli
from config.configBusiness import ai_make_config
import config.ImageLayout as imageLayout
import log.logger as log


def template_id_51(templ_parm, list_gif, list_gif_2, list_png, list_music, upload_folder, random_key):
    # 通过 random.sample 获取 list_gif 中的 16 个元素的 JSON 对象
    # 生成一个随机数，要求随机数大于等于 templ_parm['gif_number'] 的一半
    total = templ_parm['gif_number']
    lost_gif_list = []
    # 重新定义gif数量

    if random_key == 4:
        lost_gif_list = random.sample(list_gif_2, total)
        for gif in lost_gif_list:
            gif['emotion'] = gif['title']

    elif random_key == 5:
        selected_gifs = random.sample(list_gif, 1)
        selected_gifs_2 = random.sample(list_gif_2, total - 1)
        for gif in selected_gifs_2:
            gif['emotion'] = gif['title']
        lost_gif_list = selected_gifs_2 + selected_gifs

    elif random_key == 6:
        selected_gifs = random.sample(list_gif, total - 1)
        selected_gifs_2 = random.sample(list_gif_2, 1)
        for gif in selected_gifs_2:
            gif['emotion'] = gif['title']
        lost_gif_list = selected_gifs + selected_gifs_2
    elif random_key == 7:
        # 计算各自的数量
        random_number = int(total * 0.5)
        erchuang_number = int(total * 0.3)
        still_number = total - random_number - erchuang_number

        # 随机选择 GIF
        selected_gifs = random.sample(list_gif, min(random_number, len(list_gif)))

        # 分离 'erchuang' 和 'still' 类型的项
        erchuang_list = [item for item in list_gif_2 if item.get('type') == 'erchuang']
        still_list = [item for item in list_gif_2 if item.get('type') == 'still']

        # 确保随机选择数量不超过可用项数
        erchuang_gifs = random.sample(erchuang_list, min(erchuang_number, len(erchuang_list)))
        still_gifs = random.sample(still_list, min(still_number, len(still_list)))

        # 给 'erchuang' 和 'still' 类型的项添加 'emotion' 属性
        for gif in erchuang_gifs + still_gifs:
            gif['emotion'] = gif['title']
        # 合并所有选中的 GIF 项
        lost_gif_list = selected_gifs + erchuang_gifs + still_gifs
        random.shuffle(lost_gif_list)
    elif random_key == 8:
        still_list = [item for item in list_gif_2 if item.get('type') == 'still']
        lost_gif_list = random.sample(still_list, total)
        for gif in lost_gif_list:
            gif['emotion'] = gif['title']
    else:
        random_number = random.randint(int(total * 0.5), total)
        chaz = total - random_number
        selected_gifs = random.sample(list_gif, random_number)
        selected_gifs_2 = random.sample(list_gif_2, chaz)
        for gif in selected_gifs_2:
            gif['emotion'] = gif['title']
        lost_gif_list = selected_gifs + selected_gifs_2
        random.shuffle(lost_gif_list)
    # gif 本地资源 >>>>>>>>>>>>>>>>>>>>>>>>>>
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in lost_gif_list]
    log.log_put(1, len(gif_paths), None)
    selected_png = random.choice(list_png)
    # TODO 使用白底
    # random_png = upload_file(selected_png['src'], upload_folder)
    random_png = videoServiceImpl.get_background(47)
    if random_key == 4:
        random_png = videoServiceImpl.get_background("51/4")
    log.log_put(2, 1, None)
    selected_music, random_music = get_music(list_music, upload_folder, templ_parm['time_len'])
    # 背景音乐 本地资源 >>>>>>>>>>>>>>>>>>>>>>>>>
    log.log_put(3, 1, None)
    background_clip = videoServiceImpl.create_bg2(templ_parm["time_len"], random_png)
    total_duration = templ_parm["time_len"]
    final_clip = None
    audio_clip = None
    # 常规模板
    if random_key <= 3:
        final_clip, audio_clip = template_peth_1(templ_parm, gif_paths, background_clip, random_music)
    elif random_key in [4, 8]:
        final_clip, audio_clip = template_peth_51_4(templ_parm, gif_paths, background_clip, random_music)
    elif random_key in [5, 6]:
        final_clip, audio_clip = template_peth_51_5(templ_parm, gif_paths, background_clip, random_music)
    elif random_key == 7:
        final_clip, audio_clip = template_peth_51_7(templ_parm, gif_paths, background_clip, random_music)

    return final_clip, audio_clip, total_duration, lost_gif_list, selected_png, selected_music


def get_music(list_music, upload_folder, time_len, retries=0):
    if retries >= 20:
        print("<< 获取音乐时长，达到最大重试次数，操作中止。")
        return {"code": -1, "data": "获取音乐时长，达到最大重试次数。"}
    selected_music = random.choice(list_music)
    random_music = upload_file(selected_music['src'], upload_folder)
    js_sc = AudioFileClip(random_music)
    if js_sc.duration > time_len:
        return selected_music, random_music
    else:
        time.sleep(4)
        return get_music(list_music, upload_folder, time_len, retries=retries + 1)


def template_peth_51_4(templ_parm, gif_paths, background_clip, random_music):
    image_layouts = imageLayout.calculate_image_positions_51_4(templ_parm['gif_number'], templ_parm['x_number'],
                                                               templ_parm['y_number'])
    gif_clips = [videoServiceImpl.gif_to_clip_51_4(gif_path, image_layout) for gif_path, image_layout in
                 zip(gif_paths, image_layouts)]
    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        loop_time = templ_parm["time_len"] // gif_duration + 1
        loop_time = int(loop_time)
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * loop_time)
    flat_position = [layout['position'] for layout in image_layouts]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    final_clip = CompositeVideoClip(
        [background_clip] + [clip.set_position(pos) for clip, pos in gif_position_pairs])
    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)
    return final_clip, audio_clip


def template_peth_51_5(templ_parm, gif_paths, background_clip, random_music):
    image_layouts = imageLayout.calculate_image_positions_51_5(20, templ_parm['x_number'], templ_parm['y_number'], 5)
    gif_clips = [videoServiceImpl.gif_to_clip_51_4(gif_path, image_layout) for gif_path, image_layout in
                 zip(gif_paths, image_layouts)]
    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        loop_time = templ_parm["time_len"] // gif_duration + 1
        loop_time = int(loop_time)
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * loop_time)
    flat_position = [layout['position'] for layout in image_layouts]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    final_clip = CompositeVideoClip(
        [background_clip] + [clip.set_position(pos) for clip, pos in gif_position_pairs])
    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)
    return final_clip, audio_clip


def template_peth_51_7(templ_parm, gif_paths, background_clip, random_music):
    image_layouts = imageLayout.calculate_image_positions_51_7(20)
    gif_clips = [videoServiceImpl.gif_to_clip_51_4(gif_path, image_layout) for gif_path, image_layout in
                 zip(gif_paths, image_layouts)]
    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        loop_time = templ_parm["time_len"] // gif_duration + 1
        loop_time = int(loop_time)
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * loop_time)
    flat_position = [layout['position'] for layout in image_layouts]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    final_clip = CompositeVideoClip(
        [background_clip] + [clip.set_position(pos) for clip, pos in gif_position_pairs])
    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)
    return final_clip, audio_clip


def template_peth_1(templ_parm, gif_paths, background_clip, random_music):
    gif_clips = [videoServiceImpl.gif_to_clip_2(gif_path, templ_parm) for gif_path in gif_paths]
    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        loop_time = templ_parm["time_len"] // gif_duration + 1
        loop_time = int(loop_time)
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * loop_time)
    clip_with = 1080 // templ_parm["x_number"]
    clip_height = 1700 // templ_parm["y_number"]
    top = 100
    position = [
        [(clip_with * j + random.randint(0, 0), clip_height * i + top + random.randint(0, 0)) for j in
         range(templ_parm["x_number"])] for i in range(templ_parm["y_number"])]
    flat_position = [pos for sublist in position for pos in sublist]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    final_clip = CompositeVideoClip(
        [background_clip] + [clip.set_position(pos) for clip, pos in gif_position_pairs])
    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)
    return final_clip, audio_clip


def template_id_4(template_id, templ_parm, list_gif, list_text, list_music, upload_folder, list_typeface):
    # TODO 测试
    # desired_dict = next((item for item in list_text if item["id"] == 441), None)
    # text_parm = videoServiceImpl.text_to_amio_4(templ_parm, desired_dict)
    text_parm = videoServiceImpl.text_to_amio_4(templ_parm, random.choice(list_text))
    selected_gifs = videoServiceImpl.gifs_nubmer(list_gif, text_parm)
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in selected_gifs]
    log.log_put(1, len(gif_paths), None)
    gif_clips = [videoServiceImpl.gif_to_clip_3(gif_path) for gif_path, text_info in
                 zip(gif_paths, text_parm['text_temp'])]
    # top1 使用拼接语段 top2 整句未分割 ， 拼接语音段更准确
    text_music = videoServiceImpl.audio_compositing(text_parm['text_temp'], upload_folder)
    # text_music = text_parm['writing_text_music']['file_path']
    selected_music = videoServiceImpl.get_background_music(list_music, text_parm)
    # top1 使用拼接语段 top2 整句未分割 ， 拼接语音段更准确
    total_duration = sum(item['duration'] for item in text_parm['text_temp'])
    # total_duration = text_parm['writing_text_music']['duration']
    background_music = upload_file(selected_music['src'], upload_folder)
    log.log_put(3, 1, None)
    font = upload_file(videoServiceImpl.get_http_font(list_typeface, 3)['src'], upload_folder)
    log.log_put(4, 1, None)
    text_clips = videoServiceImpl.create_text(text_parm['text_temp'], templ_parm, font)
    selected_png = {"id": "无"}
    make_png = videoServiceImpl.get_background(template_id)
    log.log_put(5, 1, None)

    background_clip = videoServiceImpl.get_background_path_to_clip(ai_make_config.scales_video['w'],
                                                                   ai_make_config.scales_video['h'],
                                                                   total_duration,
                                                                   make_png)
    music_clips = AudioFileClip(text_music).subclip(0, total_duration).volumex(
        templ_parm['clip_background_text_volume'])
    background_music_clips = AudioFileClip(background_music).subclip(0, total_duration).volumex(
        templ_parm['clip_background_music_volume'])

    # 初始化时间偏移
    offset = 0
    text_clips_list = []
    gif_clips_list = []
    # 在空白视频上轮播添加每个 GIF 和每段文本
    for text_clip, gif_clip, text_info in zip(text_clips, gif_clips, text_parm['text_temp']):
        text_duration = text_info['duration']
        gif_duration = gif_clip.duration
        # 在 final_clip 上添加 text_clip
        y_position = int(background_clip.h * 0.2) - text_info['total_text_height']
        text_clips_list.append(
            text_clip.set_start(offset).set_end(offset + text_duration).set_position(("center", y_position)))
        loop_time = text_duration // gif_duration + 1
        loop_time = int(loop_time)
        gif_clip = concatenate_videoclips([gif_clip] * loop_time)
        # 重复 gif_clip 并添加到 gif_clips_list
        gif_clips_list.append(gif_clip.set_start(offset).set_end(offset + text_duration))
        offset += text_duration

    # 使用 concatenate_videoclips 拼接所有的 gif_clip
    final_gif_clip = concatenate_videoclips(gif_clips_list)
    # 将 final_clip 添加到背景图上
    final_clip = CompositeVideoClip([background_clip, final_gif_clip.set_position("center")] + text_clips_list)
    audio_clip = CompositeAudioClip([music_clips, background_music_clips])
    return text_parm, final_clip, audio_clip, total_duration, selected_gifs, selected_png, selected_music


def template_id_16(templ_parm, list_gif, list_png, list_text, list_music, upload_folder, list_typeface):
    # 测试 TODO
    # 获取文字
    text_parm = random.choice(videoServiceImpl.text_to_scene(list_text, templ_parm['copy_scene']))
    # 获取gif
    selected_gifs = videoServiceImpl.gifs_nubmer_16(list_gif, text_parm, templ_parm['gif_number'])
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in selected_gifs]
    log.log_put(1, len(gif_paths), None)
    selected_png = videoServiceImpl.get_png_16(list_png, text_parm)
    random_png = upload_file(selected_png['src'], upload_folder)
    log.log_put(2, 1, None)
    selected_music = videoServiceImpl.get_background_music(list_music, text_parm)
    random_music = upload_file(selected_music['src'], upload_folder)
    log.log_put(3, 1, None)
    background_clip = videoServiceImpl.create_bg_16(templ_parm["time_len"], random_png)
    bg_width, bg_height = background_clip.size
    gif_clips = [videoServiceImpl.gif_to_clip_16(gif_path, templ_parm) for gif_path in gif_paths]
    final_clips = [background_clip]  # 首先添加背景视频
    for i in range(len(gif_clips)):
        gif_width, gif_height = gif_clips[i].size
        pos_x = (bg_width - gif_width) // 2
        black_border_height = bg_height * 0.3  # 黑色边框的高度
        pos_y = bg_height - black_border_height - gif_height
        gif_duration = gif_clips[i].duration
        loop_time = templ_parm["time_len"] // gif_duration + 1
        loop_time = int(loop_time)
        # 将GIF动画及其位置添加到列表中
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * loop_time)
        final_clips.append(gif_clips[i].set_position((pos_x, pos_y)))
    font = upload_file(videoServiceImpl.get_http_font(list_typeface, 7)['src'], upload_folder)
    log.log_put(4, 1, None)
    text_clips = videoServiceImpl.create_text_clips_16(text_parm['writing'], templ_parm, font)

    final_clip = CompositeVideoClip(final_clips + text_clips)
    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)
    total_duration = templ_parm["time_len"]
    return text_parm, final_clip, audio_clip, total_duration, selected_gifs, selected_png, selected_music


def template_id_54(templ_parm, list_gif, list_png, list_text, list_music, list_avatar, list_typeface, upload_folder):
    # 获取情侣文案
    text_parm = random.choice(videoServiceImpl.text_to_scene(list_text, templ_parm['copy_scene']))
    # 获取gif
    selected_gifs = videoServiceImpl.gifs_nubmer_16(list_gif, text_parm, templ_parm['gif_number'])
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in selected_gifs]
    log.log_put(1, len(gif_paths), None)
    selected_png = videoServiceImpl.get_png_16(list_png, text_parm)
    random_png = upload_file(selected_png['src'], upload_folder)
    log.log_put(2, 1, None)
    make_png, gif_bottom, selected_avatars = videoServiceImpl.get_background_54(text_parm, templ_parm, upload_folder,
                                                                                random_png, list_avatar, list_typeface)
    selected_music = videoServiceImpl.get_background_music(list_music, text_parm)
    random_music = upload_file(selected_music['src'], upload_folder)
    log.log_put(3, 1, None)
    background_clip = videoServiceImpl.get_background_path_to_clip(1170, 2532, templ_parm["time_len"], make_png)

    gif_clips, position = videoServiceImpl.gif_to_clip_54(gif_paths, templ_parm, gif_bottom)
    gif_position_pairs = list(zip(gif_clips, position))
    final_clip = CompositeVideoClip(
        [background_clip] + [clip.set_position(pos) for clip, pos in gif_position_pairs])
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)

    return text_parm, final_clip, audio_clip, templ_parm[
        "time_len"], selected_gifs, selected_png, selected_music, selected_avatars


def template_id_47(template_id, templ_parm, list_gif, list_music, list_typeface, upload_folder):
    selected_gifs = random.sample(list_gif, templ_parm['gif_number'])
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in selected_gifs]
    log.log_put(1, len(gif_paths), None)
    make_png = videoServiceImpl.get_background(template_id)
    log.log_put(5, 1, None)
    selected_music = random.choice(list_music)
    random_music = upload_file(selected_music['src'], upload_folder)
    log.log_put(3, 1, None)
    texts = repCli.get_open_ai_to_text({"id": template_id, "number": templ_parm['gif_number']})
    font = upload_file(videoServiceImpl.get_http_font(list_typeface, 11)['src'], upload_folder)
    log.log_put(4, 1, None)
    background_clip = videoServiceImpl.get_background_path_to_clip(ai_make_config.scales_video['w'],
                                                                   ai_make_config.scales_video['h'],
                                                                   templ_parm['time_len'], make_png)
    gif_clips = videoServiceImpl.gif_to_clip_47(gif_paths, templ_parm)
    txt_clips = videoServiceImpl.get_txt_to_clip_47(texts, templ_parm, font)

    display_duration = templ_parm['duration']
    # 设置每个GIF和文本剪辑的开始时间和持续时间
    start_time = 0
    text_clips_list = []
    gif_clips_list = []
    while True:
        for gif_clip, txt_clip in zip(gif_clips, txt_clips):
            # 设置GIF和文本剪辑的开始时间和持续时间n)
            gif_clips_list.append(gif_clip.set_start(start_time).set_end(start_time + display_duration))
            text_clips_list.append(txt_clip.set_start(start_time).set_end(start_time + display_duration))
            start_time += display_duration
            if start_time >= templ_parm['time_len'] - templ_parm['last_time']:
                break
            # 在while循环中添加退出条件，防止无限循环
        if start_time >= templ_parm['time_len'] - templ_parm['last_time']:
            break

    final_text_clip = concatenate_videoclips(text_clips_list)
    final_gif_clip = concatenate_videoclips(gif_clips_list)
    final_clip = CompositeVideoClip([background_clip, final_gif_clip.set_position("center"),
                                     final_text_clip.set_position(("center", ai_make_config.scales_video['h'] * 0.65))])
    audio_clip = AudioFileClip(random_music).subclip(0, templ_parm["time_len"]).volumex(0.8)

    text_parm = {"id": "无"}
    selected_png = {"id": "无"}
    return text_parm, final_clip, audio_clip, templ_parm["time_len"], selected_gifs, selected_png, selected_music


def template_id_48(template_id, templ_parm, list_gif, list_gif_2, list_music, upload_folder):
    total = templ_parm['gif_number']
    # 计算各自的数量
    yc = int(total * 0.6)
    ec = total - yc
    # 随机选择 GIF
    selected_gifs = random.sample(list_gif, min(yc, len(list_gif)))
    # 确保随机选择数量不超过可用项数
    erchuang_gifs = random.sample(list_gif_2, min(ec, len(list_gif_2)))
    for gif in erchuang_gifs:
        gif['emotion'] = gif['title']
    # 合并所有选中的 GIF 项
    lost_gif_list = selected_gifs + erchuang_gifs
    random.shuffle(lost_gif_list)
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in lost_gif_list]
    log.log_put(1, len(gif_paths), None)
    make_png = videoServiceImpl.get_background(template_id)
    log.log_put(5, 1, None)
    display_duration = templ_parm['time_len']
    selected_music, random_music = get_music(list_music, upload_folder, display_duration)
    log.log_put(3, 1, None)
    background_clip = videoServiceImpl.get_background_path_to_clip(ai_make_config.scales_video['w'],
                                                                   ai_make_config.scales_video['h'],
                                                                   display_duration, make_png)
    image_layouts = imageLayout.calculate_image_positions_51_4(templ_parm['gif_number'], templ_parm['x_number'],
                                                               templ_parm['y_number'], top_bottom_margin=200)
    gif_clips = [videoServiceImpl.gif_to_clip_51_4(gif_path, image_layout) for gif_path, image_layout in
                 zip(gif_paths, image_layouts)]
    gif_clips_2 = [videoServiceImpl.gif_to_clip_2(gif_path, templ_parm) for gif_path in gif_paths]
    offset = 4
    gif_clips_list = []
    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        target_duration = templ_parm["time_len"]
        loop_time = target_duration // gif_duration + 1
        # 创建一个循环播放的 GIF
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * int(loop_time))
        gif_clips_2[i] = concatenate_videoclips([gif_clips_2[i]] * int(loop_time))
        gif_clips[i] = gif_clips[i].set_start(0).set_end(4)
        gif_clips_list.append(gif_clips_2[i].set_start(offset).set_end(offset + 1))
        offset += 1

    final_gif_clip = concatenate_videoclips(gif_clips_list)

    flat_position = [layout['position'] for layout in image_layouts]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    # 创建组合视频剪辑
    final_clip = CompositeVideoClip(
        [background_clip] +
        [clip.set_position(pos) for clip, pos in gif_position_pairs] +  # 封面内容
        [final_gif_clip.set_position("center").set_start(4)]  # 从第四秒开始的内容

    )

    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, display_duration).volumex(0.8)

    text_parm = {"id": "无"}
    selected_png = {"id": "无"}
    return text_parm, final_clip, audio_clip, display_duration, selected_gifs, selected_png, selected_music


def template_id_49(template_id, templ_parm, list_gif_2, list_music, upload_folder):
    total = templ_parm['gif_number']
    # 随机选择 GI
    # 确保随机选择数量不超过可用项数
    erchuang_list = [item for item in list_gif_2 if item.get('type') == 'erchuang']
    lost_gif_list = random.sample(erchuang_list, min(total, len(erchuang_list)))
    for gif in lost_gif_list:
        gif['emotion'] = gif['title']
    # 合并所有选中的 GIF 项
    random.shuffle(lost_gif_list)
    gif_paths = [upload_file(pam['src'], upload_folder) for pam in lost_gif_list]
    log.log_put(1, len(gif_paths), None)
    make_png = videoServiceImpl.get_background(template_id)
    log.log_put(5, 1, None)
    display_duration = templ_parm['time_len']
    selected_music, random_music = get_music(list_music, upload_folder, display_duration)
    log.log_put(3, 1, None)
    background_clip = videoServiceImpl.get_background_path_to_clip(ai_make_config.scales_video['w'],
                                                                   ai_make_config.scales_video['h'],
                                                                   display_duration, make_png)
    image_layouts = imageLayout.calculate_image_positions_51_4(templ_parm['gif_number'], templ_parm['x_number'],
                                                               templ_parm['y_number'], top_bottom_margin=400,
                                                               side_margin=0,
                                                               gap=0)
    gif_clips = [videoServiceImpl.gif_to_clip_51_4(gif_path, image_layout) for gif_path, image_layout in
                 zip(gif_paths, image_layouts)]

    for i in range(len(gif_clips)):
        gif_duration = gif_clips[i].duration
        target_duration = templ_parm["time_len"]
        loop_time = target_duration // gif_duration + 1
        # 创建一个循环播放的 GIF
        gif_clips[i] = concatenate_videoclips([gif_clips[i]] * int(loop_time))

    flat_position = [layout['position'] for layout in image_layouts]
    gif_position_pairs = list(zip(gif_clips, flat_position))
    # 创建组合视频剪辑
    final_clip = CompositeVideoClip(
        [background_clip] +
        [clip.set_position(pos) for clip, pos in gif_position_pairs]

    )

    # 加载背景音乐
    audio_clip = AudioFileClip(random_music).subclip(0, display_duration).volumex(0.8)

    text_parm = {"id": "无"}
    selected_png = {"id": "无"}
    return text_parm, final_clip, audio_clip, display_duration, lost_gif_list, selected_png, selected_music


if __name__ == '__main__':
    print("你好")
